package com.boot.security.server.controller.mffl.MfflLawyer;

import com.boot.security.server.dao.SysSmsDao;
import com.boot.security.server.dao.UserDao;
import com.boot.security.server.dao.mffl.MfflConsultationDao;
import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MfflBankcard;
import com.boot.security.server.model.mffl.MfflUser;
import com.boot.security.server.model.mffl.SysSms;
import com.boot.security.server.model.mffl.Vo.MfflConsultationVo;
import com.boot.security.server.model.mffl.Vo.MfflInformationVo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.boot.security.server.service.impl.mffl.Mfflbankcard.BankCardService;
@Api(tags = "律师功能接口(律师端)")
@RestController
@RequestMapping("/client/mfflLawyer")
public class Client_LawyerController {


    @Autowired
    private MfflUserDao mfflUserDao;

    @Autowired
    private MfflConsultationDao mfflConsultationDao;

    //银行卡
    @Autowired
    private BankCardService BankCardService;
    //短信
    @Autowired
    private SysSmsDao syssmsDao;

    //用户表
    @Autowired
    MfflUserDao mffluserdao;

    //系统表
    @Autowired
    private UserDao userDao;

    @GetMapping("/getWorkbench")
    @ApiOperation(value = "律师-工作台")
    @ApiImplicitParams({
            @ApiImplicitParam(required = true, name = "offset", value = "开始条数", dataType = "int"),
            @ApiImplicitParam(required = true, name = "limit", value = "每页条数", dataType = "int"),
            @ApiImplicitParam(required = false, name = "freeOrPaid", value = "咨询类型 0:免费咨询 1:专业咨询", paramType = "query", dataType = "int"),
            @ApiImplicitParam(required = false, name = "type", value = "问题类型", paramType = "query", dataType = "int"),
            @ApiImplicitParam(required = false, name = "rank", value = "智能排序(默认空综合排序)  1:价格 2:观看量 3:评论量", paramType = "query", dataType = "int")})
    public ReturnBean<MfflConsultationVo> getWorkbench(PageTableRequest request) {
        if (request.getOffset() == null) {
            return new ReturnBean("201", "offset错误");
        }
        if (request.getLimit() == null) {
            return new ReturnBean("201", "limit错误");
        }
        if (request.getParams().get("rank") == null) {
            request.getParams().put("rank","mc.createTime desc,mc.money desc,mc.hot desc,mc.replyNumber desc");
        }else{
            if (request.getParams().get("rank").equals("1")){
                request.getParams().put("rank","mc.money desc");
            }else if (request.getParams().get("rank").equals("2")){
                request.getParams().put("rank","mc.hot desc");
            }else if (request.getParams().get("rank").equals("3")){
                request.getParams().put("rank","mc.replyNumber desc");
            }else {
                request.getParams().put("rank","mc.createTime desc,mc.money desc,mc.hot desc,mc.replyNumber desc");
            }
        }
        PageTableResponse tableResponse = new PageTableHandler(new PageTableHandler.CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflConsultationDao.getWorkbenchCount(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {
            @Override
            public List<MfflConsultationVo> list(PageTableRequest request) {
                return mfflConsultationDao.getWorkbenchList(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
        ReturnBean returnBean = new ReturnBean();
        returnBean.setDatas(tableResponse);
        return returnBean;
    }

    @PostMapping("/saveLawyerMessage")
    @ApiOperation(value = "律师-律师认证")
    public ReturnBean saveLawyerComment(@ApiParam(value = "真实姓名", name = "realname", required = true) @RequestParam String realname,
                                        @ApiParam(value = "执业证号", name = "workcode", required = true) @RequestParam String workcode,
                                        @ApiParam(value = "所在地区（例：黑龙江省哈尔滨市）", name = "areaName", required = true) @RequestParam String areaName,
                                        @ApiParam(value = "地区编号（市级别编号）", name = "comid", required = true) @RequestParam Integer comid,
                                        @ApiParam(value = "所在律所", name = "lawfirm", required = true) @RequestParam String lawfirm,
                                        @ApiParam(value = "执业证照片地址(正面反面，分隔)", name = "workBootImage", required = true) @RequestParam String workBootImage,
                                        @ApiParam(value = "身份证号", name = "idnumber", required = true) @RequestParam String idnumber,
                                        @ApiParam(value = "身份证照片地址(正面反面，分隔)", name = "idnumberImage", required = true) @RequestParam String idnumberImage,
                                        @ApiParam(value = "律所编号", name = "licensenumber", required = true) @RequestParam String licensenumber) {


        if (StringUtils.isEmpty(realname)) {
            return new ReturnBean("201", "真实姓名不可为空");
        }

        if (StringUtils.isEmpty(workcode)) {
            return new ReturnBean("201", "执业证号不可为空");
        }
        if (StringUtils.isEmpty(areaName)) {
            return new ReturnBean("201", "所在地区不可为空");
        }
        if (comid == null) {
            return new ReturnBean("201", "地区编号不可为空");
        }
        if (StringUtils.isEmpty(lawfirm)) {
            return new ReturnBean("201", "所在律所不可为空");
        }
        if (StringUtils.isEmpty(workBootImage)) {
            return new ReturnBean("201", "执业证照片不可为空");
        }
        if (StringUtils.isEmpty(idnumber)) {
            return new ReturnBean("201", "身份证号不可为空");
        }
        if (StringUtils.isEmpty(idnumberImage)) {
            return new ReturnBean("201", "身份证照片不可为空");
        }
        if (StringUtils.isEmpty(licensenumber)) {
            return new ReturnBean("201", "律所编号不可为空");
        }

        SysUser sysUser = UserUtil.getLoginUser();
        MfflUser mfflUser = this.mfflUserDao.getByUserId(sysUser.getId());
        mfflUser.setRealname(realname);
        mfflUser.setWorkCode(workcode);
        mfflUser.setAreaName(areaName);
        mfflUser.setComid(comid);
        mfflUser.setLawfirm(lawfirm);
        mfflUser.setWorkBootImage(workBootImage);
        mfflUser.setIdnumber(idnumber);
        mfflUser.setIdnumberImage(idnumberImage);
        mfflUser.setLawyerInspect(2);
        mfflUser.setLicensenumber(licensenumber);
        int status = this.mfflUserDao.update(mfflUser);
        if (status == 0) {
            return new ReturnBean("201", "修改失败");
        }
        return new ReturnBean();
    }

    @PostMapping("/updateLawyerMessage")
    @ApiOperation(value = "律师-修改律师信息(领域，律师寄语，教育背景，认证资质)")
    public ReturnBean saveLawyerComment(@ApiParam(value = "律师擅长领域，分隔", name = "field") @RequestParam(required = false) String field,
                                        @ApiParam(value = "律师寄语", name = "content") @RequestParam(required = false) String content,
                                        @ApiParam(value = "教育背景", name = "education") @RequestParam(required = false) String education,
                                        @ApiParam(value = "认证资质", name = "thirdlevel") @RequestParam(required = false) String thirdlevel) {

        SysUser sysUser = UserUtil.getLoginUser();
        MfflUser mfflUser = this.mfflUserDao.getByUserId(sysUser.getId());
        if (!StringUtils.isEmpty(field)){
            mfflUser.setField(field);
        }
        if (!StringUtils.isEmpty(content)){
            mfflUser.setContent(content);
        }
        if (!StringUtils.isEmpty(education)){
            mfflUser.setEducation(education);
        }
        if (!StringUtils.isEmpty(thirdlevel)){
            mfflUser.setThirdlevel(thirdlevel);
        }
        int status = this.mfflUserDao.update(mfflUser);
        if (status == 0) {
            return new ReturnBean("201", "修改失败");
        }
        return new ReturnBean();
    }

    @GetMapping(value = "showbankcard")
    @ApiOperation(value="获取银行卡信息")
    public ReturnBean<MfflBankcard> showBankCard(){
        return BankCardService.showBankCard();
    }


    @PostMapping(value = "setupbankcard")
    @ApiOperation(value="设置银行信息(测试验证码☞a54g2jc)")
    public ReturnBean SetupBankCard(@ApiParam(name = "code",value = "验证码",required = true)@RequestParam String code,
                                    @ApiParam(name = "bankcardnumber",value = "银行卡号",required = true)@RequestParam String bankcardnumber,
                                    @ApiParam(name = "bank",value = "开户行",required = true)@RequestParam String bank,
                                    @ApiParam(name = "realname",value = "真实姓名",required = true)@RequestParam String realname){
       if(StringUtils.isEmpty(code)){
            return new ReturnBean("201", "验证码不可为空");
        }if(StringUtils.isEmpty(bankcardnumber)){
            return new ReturnBean("201", "银行卡号不可为空");
        }if(StringUtils.isEmpty(bank)){
            return new ReturnBean("201", "开户行不可为空");
        }if(StringUtils.isEmpty(realname)){
            return new ReturnBean("201", "真实姓名不可为空");
        }
        //获取当前用户
        SysUser sysUser = UserUtil.getLoginUser();
        //查询用户
        SysUser byId = userDao.getById(sysUser.getId());


        if(!"a54g2jc".equals(code)){
            //判断验证码是否过期
            if(StringUtils.isEmpty(byId.getPhone())){
                return new ReturnBean("201", "您未设置手机号");
            }
            SysSms byPhone = syssmsDao.getByPhone(Long.parseLong(byId.getPhone()));
            if(byPhone == null){
                return new ReturnBean("101", "您还未发送验证码");
            }

            long startTime = byPhone.getSendtime().getTime();
            long endTime = System.currentTimeMillis();

            if ((endTime - startTime) > ((1000 * 60) * 10)) {
                return new ReturnBean("101", "验证码过期");
            }else{
                if (!byPhone.getCode().equals(code)) {
                    return new ReturnBean("101", "验证码不正确");
                }
            }
        }
        return BankCardService.SetupBankCard(bankcardnumber,bank,realname);
    }


}
